<?php
// +----------------------------------------------------------------------
// | 文件说明：幻灯片
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: wuwu <15093565100@163.com>
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Date: 2017-5-25
// +----------------------------------------------------------------------
namespace api\home\controller;

use cmf\controller\RestBaseController;
use think\Config;
use think\Validate;
use think\Db;
use cmf\phpqrcode\QRtoolsjkl;
use api\home\model\SlideModel;

class LoginController extends RestBaseController
{

    /**
     * [read description]
     * @return [type] [description]
     */
    public function wech_code()
    {
        $redirect_uri = 'https://sell.wengcong.cn/api/wechRedirect';
        $appId = 'wx06eaa250b6fa7688';
        $rn = mt_rand(100,999);
        echo $appid = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appId.'&redirect_uri='.$redirect_uri.'&response_type=code&scope=snsapi_userinfo&state=1&connect_redirect='.$rn.'#wechat_redirect';
    }

    /**
     * [wechat_redirect 微信扫码登陆回调地址]
     * @return [type] [description]
     */
    public function wechat_redirect(){
        
    }

    /**
     * [login 用户登陆]
     * @return [type] [description]
     */
    public function login(){
        /**验证**/
        $validate = new Validate([
            'email' => 'require',
            'password' => 'require',
            'captcha' => 'require'
        ]);
        $validate->message([
            'email.require' => '邮箱不能为空!',
            'password.require' => '请输入您的密码!',
            'captcha.require' => '验证码不能为空！'
        ]);

        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }
        /**
         * 检测验证码
         */
        // if (!cmf_captcha_check($data['captcha'])) {
        //     $this->error(lang('CAPTCHA_NOT_RIGHT'));
        // }
        /**条件**/
        $userWhere = [];
        if (Validate::is($data['email'], 'email')) {
            $userWhere['email'] = $data['email'];
        } else {
            $this->error("请输入正确的邮箱格式！");
        }
        $user = Db::table('a_user')->where($userWhere)->find();
        if(!$user){
            $this->error('账号不存在！');
        }
        $userWhere['password'] = md5($data['password']);
        $pas = Db::table('a_user')->where($userWhere)->find();
        if(!$pas){
            $this->error('密码错误！');    
        }
        if($pas['emailStatus'] == 1){
            $this->error('你还未通过邮箱激活!');
        }
        // if($pas['wachStatus'] == 1){
        //     $this->error('你还未绑定个人微信号!');
        // }
        $token = time().'TK'.$pas['id'];
        Db::table('a_user')->where(['id'=>$pas['id']])->update(['session'=>$token]);
        $this->success('登陆成功!',$token); 
    }

    /**
     * [logginOut 退出登录]
     * @return [type] [description]
     */
    public function loginOut(){
        $token =  input('token') ? input('token') : $_SERVER['HTTP_TOKEN'];
        if(!$token){
            $this->error('缺少参数:token');
        }
        $data = Db::table('a_user')->where(['session'=>$token])->update(['session'=>'']);
        if($data){
            $this->success('退出成功！');
        }
        $this->error('token有误!');
    }
}